import { getPickingOrders,getPickingOrder } from '@/api/picking-order'
import Pagination from '@/components/Pagination/index'


export default {
  name: 'Packing-Item-List',
  components: { Pagination },

  data(){
    return{
      listLoading: true,
      dto: null,
      order: null,
      list: null,
      checked_list: [],
      msg: '',
      audio: new Audio(),
      error_list: [],
      all_checked: false
    }
  },

  created(){
    this.audio.src = 'https://cdn.qianqianying.com/audio/error.wav'
    this.getData()
  },

  onShow(){
  },

/**
  watch:{
    msg:{
      handler(oldValue, newValue){
        var arry = []
        arry.push(this.msg)
        for(var i = 0; i < arry.length; i++){
          //...做判断，调接口什么的，随业务逻辑写了
        }
       }
    },
    immediate: true
  },
*/

  methods: {

    getData() {
      const id = this.$route.query.id
      const order_id = this.$route.query.order_id
      let checked_list = {}
      getPickingOrder(id).then(response => {
        this.dto = response.picking_order
        this.list = this.dto.orders
        this.dto.orders.forEach(item => {
          if (order_id == item.id) {
            this.order = item
            item.items.forEach(sitem => {
              checked_list['' + sitem.product.item_id] = {
                'sku': sitem.product.sku,
                'checked': false,
                'unchecked_num': sitem.quantity,
                'checked_num': 0,
                'total': sitem.quantity
              }
            })
            this.checked_list = checked_list
            this.list = item.items

            var b = ""
            var _this = this
            document.onkeydown = function() {
              if (event.keyCode != 13) {
                var bizCode = String.fromCharCode(event.keyCode);
                if (event.keyCode >= 48 && event.keyCode <= 122) {
                  b = b + bizCode
                }
              } else {
                let checked_list = _this.checked_list
                let flag = false
                for(var i in checked_list){
                  if(b == checked_list[i]['sku']){
                    flag = true
                    if(checked_list[i]['checked_num'] < checked_list[i]['total']){
                      checked_list[i]['checked_num'] = checked_list[i]['checked_num'] + 1
                      checked_list[i]['unchecked_num'] = checked_list[i]['unchecked_num'] - 1
                    }else{
                      _this.$message.error('该产品已超出拣货数量')
                      _this.audio.play()
                      _this.audio.play()
                    }
                    if(checked_list[i]['checked_num'] == checked_list[i]['total']){
                      checked_list[i]['chceked'] = true
                    }
                    _this.checked_list = checked_list
                    _this.checkAll()
                  }
                }
                if(!flag){
                  _this.$message.error('不存在订单列表中')
                  _this.audio.play()
                }
                b = ""
              }
            }
          }
        })
        this.listLoading = false
      }).catch(error => {
        this.dto = null
        this.listLoading = false
      })
    },

    auto_confirm(id) {
      let checked_list = this.checked_list
      checked_list['' + id].checked = true
      checked_list['' + id].unchecked_num = 0
      checked_list['' + id].checked_num = checked_list['' + id].total
      this.checked_list = checked_list
      this.checkAll()
    },

    checkAll(){
      let checked_list = this.checked_list
      let flag = true
      for (let key in checked_list) {
        if(!checked_list[key].checked){
          flag = false
          break
        }
      }
      if(flag){
        this.$alert('拣货完成', '提示', {
          confirmButtonText: '确定',
        });
      }
    }
  }
}
